﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PB.Enums;
using System.Xml.Linq;
using PB.Base;
using System.Text.RegularExpressions;
using System.IO;

namespace PB.Helpers
{
    public static class Utilities
    {
        public static DateTime GetXmlDateTime(string value)
        {
            string[] parts = value.Split('-');

            string[] date = parts.GetValue(0).ToString().Split('/');
            string[] time = parts.GetValue(1).ToString().Split(':');

            int day = Convert.ToInt32(date.GetValue(0));
            int month = Convert.ToInt32(date.GetValue(1));
            int year = Convert.ToInt32(date.GetValue(2));

            int hour = Convert.ToInt32(time.GetValue(0));
            int minute = Convert.ToInt32(time.GetValue(1));
            int second = Convert.ToInt32(time.GetValue(2));

            DateTime dt = new DateTime(year, month, day, hour, minute, second);

            return dt;
        }

        public class EnumLogs
        {
            public EnumLogs(LogType logType)
            {
                string selEnum = Enum.GetName(typeof(LogType), logType);

                string element = selEnum;
                string elements = selEnum == "Info" ? elements = "Informations" : elements = selEnum + "s"; // aggiunge una 's' mette il plurale

                this.NameElement = element;
                this.NameElements = elements;
            }

            public string NameElement { get; private set; }
            public string NameElements { get; private set; }
        }

        public static User GetUserLog(XElement xelement)
        {
            var userAttr = xelement.Attribute("User");

            User user = null;

            if (userAttr != null)
            {
                user = XmlHelper.Users.GetUsers().Where(u => u.Username == userAttr.Value).SingleOrDefault();
            }

            return user;
        }

        public static bool IsNull(this DateTime dt)
        {
            if (dt.Year == 1 && dt.Day == 1 && dt.Month == 1 &&
                dt.Hour == 0 && dt.Minute == 0 && dt.Second == 0)
            {
                return true;
            }

            return false;
        }

        public static string CheckFilename(this string path)
        {
            Regex fnReg = new Regex(@"^(([a-zA-Z]:)|(\))(\{1}|((\{1})[^\]([^/:*?<>""|]*))+)$");

            if (!Directory.Exists(Path.GetDirectoryName(path)))
            {
                throw new ArgumentException("In this path, the directory not exists.");
            }
            else
            {
                string filename = Path.GetFileName(path);

                if (!String.IsNullOrWhiteSpace(filename))
                {
                    throw new ArgumentException("In the path, the filename is required.");
                }
                else
                {
                    if (!fnReg.IsMatch(filename))
                    {
                        throw new ArgumentException("This filename isn't valid.");
                    }                    
                }
            }

            return path;
        }

        public static string CheckedPathXml(string path)
        {
            string extension = Path.GetExtension(path);

            if (String.IsNullOrWhiteSpace(extension))
                throw new ArgumentException("This 'xml' extension is required.");

            else if (extension != ".xml")
                throw new ArgumentException("The extension isn't valid. It must be 'xml'.");

            return path;
        }
    }
}
